<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="角色名" v-show="isShow">
                <el-select v-model="ruleForm.RoleID" placeholder="请选择角色">
                    <el-option :label="item.Rname" v-for="(item, index) in role" :key="index" :value="6"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="姓名" prop="UserName">
                <el-input v-model="ruleForm.UserName"></el-input>
            </el-form-item>

            <el-form-item label="性别" prop="Sex">
                <el-radio-group v-model="ruleForm.Sex">
                    <el-radio label="男" name="sex"></el-radio>
                    <el-radio label="女" name="sex"></el-radio>
                </el-radio-group>
            </el-form-item>

            <el-form-item label="出生日期" required>
                <el-col :span="11">
                    <el-form-item prop="Birthday">
                        <el-date-picker value-format="yyyy-MM-dd" placeholder="选择日期" v-model="ruleForm.Birthday"
                            style="width: 100%;"></el-date-picker>
                    </el-form-item>
                </el-col>
            </el-form-item>

            <el-form-item label="手机号码" prop="Phone">
                <el-input v-model="ruleForm.Phone"></el-input>
            </el-form-item>
            <el-form-item label="账户" prop="LoginName">
                <el-input v-model="ruleForm.LoginName"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="LoginPwd">
                <el-input v-model="ruleForm.LoginPwd"></el-input>
            </el-form-item>

            <el-form-item label="状态" prop="Status">
                <el-switch v-model="ruleForm.Status"></el-switch>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>




    </div>

</template>

<script>

export default {
    name: 'add',
    data() {
        return {
            role: [],
            ruleForm: {
                UserName: "狗华",
                Sex: "男",
                Birthday: "",
                ImgID: 1,
                RoleID: 6,
                Phone: "",
                LoginName: "",
                LoginPwd: "",
                Status: true
            },
            rules: {
                UserName: [
                    { required: true, message: '请输入用户信息', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                RoleID: [
                    { required: true, message: '请选择角色', trigger: 'change' }
                ],
                Birthday: [
                    { required: true, message: '请选择日期', trigger: 'change' }
                ],
                Sex: [
                    { required: true, message: '请选择性别', trigger: 'change' }
                ],
                Phone: [
                    { required: true, message: '请输入手机号码', trigger: 'blur' },
                    { min: 11, max: 11, message: '长度在必须为11位', trigger: 'blur' }
                ],
                LoginName: [
                    { required: true, message: '请输入账户', trigger: 'blur' }
                ],
                LoginPwd: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ],
            }
        }
    },
    model: {
        prop: "dialogFormVisible",
        event: "change",
    },
    props: { dialogAdd: Boolean },
    methods: {
        submitForm(formName) {
            //先验证表单数据
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    /* alert(JSON.stringify(this.ruleForm)) */
                    this.axios({
                        method: 'post',
                        url: 'Add',
                        data: this.ruleForm
                    }).then(res => {
                        if (res.code > 0) {

                            this.$emit('update:dialogAdd', false)
                            //刷新主页面数据
                            this.$emit("refresh");
                            //关闭弹窗
                            this.$emit("change", false);
                            this.$message({ type: 'success', message: res.msg })

                        } else {
                            this.$message({ type: 'danger', message: res.msg })
                        }
                    })

                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }

    },
    created() {
        this.axios({
            method: 'get',
            url: 'Role',
            params: null,
        }).then(res => {
            this.role = res.data
        })
    }
}
</script>

<style scoped></style>